package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import com.cloud.bc.model.entity.BcAuthentications;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.entity.BcUserPhotos;
import com.cloud.bc.mapper.BcUserPhotosMapper;
import com.cloud.bc.model.enums.PhotoEnum;
import com.cloud.bc.service.BcAuthenticationsService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUserPhotosService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * 用户上传图片
 *
 * @author douhaichao code generator
 * @date 2021-01-05 11:22:21
 */
@Service
@AllArgsConstructor
public class BcUserPhotosServiceImpl extends ServiceImpl<BcUserPhotosMapper, BcUserPhotos> implements BcUserPhotosService {
    private final BcAuthenticationsService bcAuthenticationsService;
    private final BcSinglesService bcSinglesService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcUserPhotos selectOne(BcUserPhotos req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcUserPhotos>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcUserPhotos> selectAll(BcUserPhotos req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcUserPhotos> selectPage(Page page, BcUserPhotos req) {
        QueryWrapper<BcUserPhotos> bcUserPhotosQueryWrapper = new QueryWrapper<>();
        bcUserPhotosQueryWrapper.setEntity(req);
        bcUserPhotosQueryWrapper.orderByDesc("id");
        IPage<BcUserPhotos> list = baseMapper.selectPage(
                page, bcUserPhotosQueryWrapper);
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcUserPhotos> selectPageByLike(Page page, BcUserPhotos req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (!StrUtil.isEmpty(req.getPhoto())) {
            queryWrapper.like("photo", req.getPhoto());
        }

        if (null != req.getIsCheck()) {
            queryWrapper.eq("is_check", req.getIsCheck());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }
        IPage<BcUserPhotos> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcUserPhotos req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public boolean setPhotoData(BcUserPhotos req) {
        //相册
        if (req.getTitle().equals(PhotoEnum.ALBUM.getTitle())) {
            //如果是相册就一直是增加
            baseMapper.insert(req);

            return true;
        }
        return true;
    }
}
